var User = $.Class.create({
	
	_loginUrl : '/login/auth/format/json',
	_logoutUrl : '/login/logout/format/json',
		
	// constructor		
	initialize : function(email) {
		this._email = email;
	},

	
	/* Handlers */
	
	addHomePageHandlers : function() {
		self = this;

		$('#btnLogin').click(function() {
			var options = {
				email: $('#email').val(),
				password: $('#password').val(),
				complete: self.completeLoginHomePage,
				beforeSend: self.beforeSendLoginHomePage,
				error: self.errorLoginHomePage
			};
			self.authenticate(options);
			return false;
		});
		
		$('#btnLogout').click(function() {
			var options = {
				complete: self.completeLogoutHomePage,
				beforeSend: self.beforeSendLogoutHomePage,
				error: self.errorLogoutHomePage
			};
			self.logout(options);
			return false;
		});		
				
	},
	
	
	/* Logout */
	
	logout : function(options) {
		if (!options.url) options.url = this._logoutUrl;		
		$.ajax({
			type: "POST",
			url: options.url,
			dataType: "json",
			beforeSend: options.beforeSend,			
			error: options.error,
		 	complete: options.complete
		});
	},	
	
	beforeSendLogoutHomePage : function(jqXHR, settings) {
		var liWidth = $('#divUserLogged').width();
		var liHeight = $('#divUserLogged').height();
		// To be sure we can set the background position
		$('#divUserLogged').css('display', 'block');
		// Hide the container with the text and button
		$('#divUserLogged div').fadeOut('fast');				
		// Setup the size of the bigger container to show the loader animation
		$('#divUserLogged').width(liWidth + 'px');		
		$('#divUserLogged').height(liHeight + 'px');
		// Show the loader animation
		$('#divUserLogged').css('background-position', '50% 50%');
		
	},
	
	completeLogoutHomePage : function(jqXHR, textStatus) {		
		$('#divUserLogged').css('background-position', '50% 1000px');
		var user = $.parseJSON(jqXHR.responseText);
		if (!user.email) {
			// Hide the loader animation container
			$('#divUserLogged').fadeOut('fast');
			// Show the login form conatiners			
			$('#liLoginLoader div').fadeIn('fast');						
			$('#liLogin div').fadeIn('fast');
		} else {
			// If it fails, show the text and logout button again
			$('#divUserLogged div').fadeIn('fast');
		}		
	},
	
	errorLogoutHomePage : function (jqXHR, textStatus, errorThrown) {
		alert("Ha ocurrido un error grave");
		$('#divUserLogged div').fadeIn('fast');
		$('#divUserLogged').css('background-position', '50% 1000px');
	},	
	
	/* Authentication */
	
	authenticate : function(options) {
		if (!options.url) options.url = this._loginUrl;		
		$.ajax({
			type: "POST",
			url: options.url,
			dataType: "json",
			data: 'email=' + options.email + '&password=' + options.password,
			beforeSend: options.beforeSend,			
			error: options.error,
		 	complete: options.complete
		});
	},
	
	
	errorLoginHomePage : function (jqXHR, textStatus, errorThrown) {
		alert("Ha ocurrido un error grave");
		$('#liLoginLoader div').fadeIn('fast');
		$('#liLoginLoader').css('background-position', '50% 1000px');
	},
	
	beforeSendLoginHomePage : function(jqXHR, settings) {
		$('#email').attr('disabled', true);
		$('#password').attr('disabled', true);
		var liWidth = $('#liLoginLoader').width();
		var liHeight = $('#liLoginLoader').height();		
		// Hide the left container with the button and link
		$('#liLoginLoader div').fadeOut('fast');			
		// To be sure that we can set the background position
		$('#liLoginLoader').css('display', 'block');
		// Setup the loader animation container size
		$('#liLoginLoader').width(liWidth + 'px');		
		$('#liLoginLoader').height(liHeight + 'px');
		// Show the loader animation
		$('#liLoginLoader').css('background-position', '50% 50%');		
	},
	
	completeLoginHomePage : function(jqXHR, textStatus) {		 		
		$('#email').attr('disabled', false);
		$('#password').attr('disabled', false);
		
		var user = $.parseJSON(jqXHR.responseText); 		
 		if (user) {
 			// Hide the loader animation
 			$('#liLoginLoader').css('background-position', '50% 1000px');
 			// Hide the entire form container
 			$('#liLogin div').fadeOut('fast');
 			// Show the user email and show the container
 			$('#divUserLogged span').html(user.email);
 			$('#divUserLogged div').show();
 			$('#divUserLogged').fadeIn('fast');
 		}
 		
 		var url = jqXHR.getResponseHeader('X-Redirect');
 		if (url) $(location).attr('href',url); 		
	}
	
}, {
	// properties
	getset : [
	          [ 'email', '_email' ],
	          [ 'loginUrl', '_loginUrl' ],
	          [ 'logoutUrl', '_logoutUrl' ]
	]
});